package org.kernely.security.ui.user.mgmt;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.data.validator.EmailValidator;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

/**
 * The view to add or edit a user.
 */
public class AddEditUserManagementView extends VerticalLayout implements IKernelyView{

	/**
	 * The Serial version ID
	 */
	private static final long serialVersionUID = 515615112548125L;
	
	private TextField login;
	private TextField password;
	private TextField name;
	private TextField firstname;
	private TextField mail;
	private NativeSelect civility;
	
	/**
	 * AddEditUserManagementView's constructor
	 */
	public AddEditUserManagementView(){
		this.login = new TextField("Login");
		this.password = new TextField("Password");
		this.password.setSecret(true);
		this.name = new TextField("Name");
		this.firstname = new TextField("Firstname");
		this.mail = new TextField("Mail");
		this.mail.addValidator(new EmailValidator("This e-mail is not valid!"));
		this.civility = new NativeSelect("Civility");
	
		this.login.setWidth(230, Sizeable.UNITS_PIXELS);
		this.password.setWidth(230, Sizeable.UNITS_PIXELS);
		this.name.setWidth(230, Sizeable.UNITS_PIXELS);
		this.firstname.setWidth(230, Sizeable.UNITS_PIXELS);
		this.mail.setWidth(230, Sizeable.UNITS_PIXELS);
		this.civility.setWidth(230, Sizeable.UNITS_PIXELS);
		
		this.login.setRequired(true);
		this.password.setRequired(true);
		this.name.setRequired(true);
		this.firstname.setRequired(true);
		this.mail.setRequired(true);
		
		this.login.setRequiredError("Login can not be empty !");
		this.password.setRequiredError("Password can not be empty !");
		this.name.setRequiredError("Name can not be empty !");
		this.firstname.setRequiredError("Firstname can not be empty !");
		this.mail.setRequiredError("Mail can not be empty !");
		
		this.addComponent(this.login);
		this.addComponent(this.password);
		this.addComponent(new Label("<hr />",Label.CONTENT_XHTML));
		this.addComponent(this.name);
		this.addComponent(this.firstname);
		this.addComponent(this.mail);
		this.addComponent(this.civility);
		this.civility.setNullSelectionAllowed(false);
		this.civility.addItem(0);
		this.civility.setItemCaption(0, "Mr.");
		this.civility.addItem(1);
		this.civility.setItemCaption(1, "Mrs.");
		this.civility.addItem(2);
		this.civility.setItemCaption(2, "Ms.");
		this.civility.setValue(0);
		
		this.login.focus();
				
		// Window's size
		this.setWidth(270, Sizeable.UNITS_PIXELS);
		this.setHeight(400, Sizeable.UNITS_PIXELS);
		
		this.setMargin(true, true, true, true);
	}
	
	/**
	 * Gets the mail field
	 * @return the mail field
	 */
	public final TextField getMail() {
		return mail;
	}

	/**
	 * @see org.kernely.ui.mvp.IKernelyView#getComponent()
	 */
	public final Component getComponent() {
		return this;
	}

	/**
	 * Gets the login field
	 * @return the login field
	 */
	public final TextField getLogin() {
		return login;
	}

	/**
	 * Gets the password field
	 * @return the password field
	 */
	public final TextField getPassword() {
		return password;
	}

	/**
	 * Gets the name field
	 * @return the name field
	 */
	public final TextField getName() {
		return name;
	}

	/**
	 * Gets the firstname field
	 * @return the firstname field
	 */
	public final TextField getFirstname() {
		return firstname;
	}

	/**
	 * Gets the civility field
	 * @return the civility field
	 */
	public final NativeSelect getCivility() {
		return civility;
	}
}
